from selenium import webdriver
from random import randint
from time import sleep
from selenium.webdriver.support.select import Select
driver = webdriver.Firefox()
driver.get('http://localhost/jxczzm/index.asp')

ipts = driver.find_elements_by_tag_name('input')
ipts[0].send_keys('admin')
ipts[1].send_keys('admin')
ipts[2].click()

sleep(2)
# 从当前默认位置切换到菜单框架
driver.switch_to.frame('left')
sysset_css = '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1) > img:nth-child(1)'
driver.find_element_by_css_selector(sysset_css).click()

# 继续
danwei_css ='#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(7) > td:nth-child(1)'
driver.find_element_by_css_selector(danwei_css).click()
sleep(2)

# 删除全部单位
driver.switch_to.default_content()
driver.switch_to.frame('right')
sleep(1)
driver.find_element_by_id('chkall').click()
shanchu_css = 'input.button:nth-child(2)'
driver.find_element_by_css_selector(shanchu_css).click()

# # 弹窗
driver.switch_to.alert.accept()
sleep(2)

# 添加单位
bt_css = 'input.button:nth-child(1)'
driver.find_element_by_css_selector(bt_css).click()
sleep(2)

# 输入单位
driver.find_element_by_name('danwei').send_keys('吨')
driver.find_element_by_name('submit').click()
sleep(2)

# 提示框
driver.switch_to.alert.accept()
sleep(2)
# 切到左
driver.switch_to.default_content()
driver.switch_to.frame('left')
# 新增大类
bm_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(5) > td:nth-child(1)'
driver.find_element_by_css_selector(bm_css).click()
# 切到右
driver.switch_to.default_content()
driver.switch_to.frame('right')
sleep(1)
bs_css = 'input.button:nth-child(1)'
driver.find_element_by_css_selector(bs_css).click()
driver.find_element_by_name('bigclass').send_keys('A')
sleep(2)
driver.find_element_by_name('submit').click()
# 窗口
driver.switch_to.alert.accept()
# 切到左
driver.switch_to.default_content()
driver.switch_to.frame('left')
sm_css ='#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(6) > td:nth-child(1)'
driver.find_element_by_css_selector(sm_css).click()
# 切到右
driver.switch_to.default_content()
driver.switch_to.frame('right')
driver.find_element_by_name('bigclass').click()
sleep(2)
sel_lst = Select(driver.find_element_by_name('bigclass'))
lst_count = len(sel_lst.options)
rand = randint(0,lst_count-1)
sel_lst.select_by_index(rand)
sleep(2)
ss_css = 'input.button:nth-child(1)'
driver.find_element_by_css_selector(ss_css).click()
driver.find_element_by_name('smallclass').send_keys('a1')
sleep(2)
driver.find_element_by_name('submit').click()
# 窗口
driver.switch_to.alert.accept()

# 先回到原始位
driver.switch_to.default_content()

# 切换到顶部框架
driver.switch_to.frame('topFrame')
driver.find_element_by_link_text('安全退出').click()
sleep(1)
# 弹出是否确认窗口
driver.switch_to.alert.accept()
sleep(2)

# 关闭
driver.quit()